package com.holy.sh.couviewholy.cvgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.View;
import android.view.ViewGroup;

import org.junit.experimental.max.MaxCore;

/**
 * Holy Email :victorhhl@163.com
 * Create by Holy on 2019/8/15
 * Description :
 */
public class FlowLayout extends ViewGroup {


    public FlowLayout(Context context) {
        super(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHight = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHightMode = MeasureSpec.getMode(heightMeasureSpec);
        int lineWidth=0;
        int lineHight=0;
        int height=0;
        int width=0;
        int count=getChildCount();
        for (int i = 0; i <count ; i++) {
            View child=getChildAt(i);
            measureChild(child,widthMeasureSpec,heightMeasureSpec);
            MarginLayoutParams lp= (MarginLayoutParams) child.getLayoutParams();
            int childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
            int childhigth=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;

            if (lineWidth + childWidth > measureWidth) {
                width = Math.max(lineWidth, width);
                height += lineHight;
                lineHight = childhigth;
                lineWidth = childWidth;
            } else {
                lineHight=Math.max(lineHight,childhigth);
                lineWidth+=childWidth;
            }

            if (i==count-1) {
                height+=lineHight;
                width = Math.max(width, lineWidth);
            }
        }

        setMeasuredDimension((measureWidthMode==MeasureSpec.EXACTLY)?measureWidth:width,
                (measureHightMode==MeasureSpec.EXACTLY)?measureHight:height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int count=getChildCount();
        int lineWidth=0;
        int lineHeigth=0;
        int top = 0,left=0;
        for (int i = 0; i <count ; i++) {
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
            int childHeight=child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin;
            if (childWidth + lineWidth > getMeasuredWidth()) {
                top += lineHeigth;
                left = 0;
                lineHeigth = childHeight;
                lineWidth = childWidth;
            } else {
                lineHeigth = Math.max(lineHeigth,childHeight);
                lineWidth+=childWidth;
            }
            int lc=left+lp.leftMargin;
            int tc=top+lp.topMargin;
            int rc=lc+child.getMeasuredWidth();
            int bc=tc+child.getMeasuredHeight();

            child.layout(lc,tc,rc,bc);

            left += childWidth;
        }

    }

    //为了获取margain值
    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
    }
}
